package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.*;
import com.itheima.dao.OrderDao;
import com.itheima.dto.OrderDto;
import com.itheima.service.*;
import com.itheima.utils.MyBaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private UserService userService;

    @Override
    public int add(Orders orders) {
        User u = userService.findById(MyBaseContext.getThreadLong());
        AddressBook byId = addressBookService.getById(orders.getAddressBookId());
        //先对象订单数据进行补充，然后进行添加
        orders.setNumber(UUID.randomUUID().toString().replace("-",""));
        orders.setStatus(1);
        orders.setUserId(MyBaseContext.getThreadLong());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(u.getPhone());
        String a = (byId.getProvinceName() !=null ? byId.getProvinceName() : "")+
                (byId.getCityName()!=null?byId.getCityName(): "")+
                (byId.getCityName()!=null?byId.getCityName(): "")+
                (byId.getDistrictName()!=null?byId.getDistrictName(): "")+
                (byId.getDetail()!=null?byId.getDetail(): "");
        orders.setAddress(a);
        orders.setUserName(u.getName());
        orders.setConsignee(byId.getConsignee());
        //调用dao进行添加操作
         int row = orderDao.insert(orders);
        //判断订单是否添加成功，成功则对订单详情表进行添加
        if (row > 0){
            //订单详情表来自购物车对象，先对购物车进行查询遍历，将购物车中的对象添加到订单详情表中
            List<ShoppingCart> list = shoppingCartService.findByUId(MyBaseContext.getThreadLong());
            System.out.println(list);
            //通过流对象查询到的购物车对象进行遍历
            int row1=0;
            for (ShoppingCart shoppingCart : list) {
                //创建订单详情表对象
                OrderDetail orderDetail = new OrderDetail();
                //将遍历的购物车菜品对象添加到订单详情表中
                BeanUtils.copyProperties(shoppingCart,orderDetail);
                //将订单id添加到订单详表的外键id中
                orderDetail.setOrderId(orders.getId());
                //调用订单详情表的id进行添加操作
                 row1 += orderDetailService.add(orderDetail);

            }
            return row1==list.size() ? 1 : 0;


        }
        return row;
    }

    public void cleanShoppingCart(){
        shoppingCartService.delete(MyBaseContext.getThreadLong());
    }

    @Override
    public IPage<Orders> page(MyPage page) {
        //创建分页查询订单的条件
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        //设置查询的条件
        qw.eq(Orders::getUserId,MyBaseContext.getThreadLong());
        //设置分页查询的页数和显示的数据行数
        IPage<Orders> iPage = new Page<>(page.getPage(),page.getPageSize());
        //获取查询的当页集合，并用流进行遍历
        List<Orders> list = iPage.getRecords();
        //对集合通过流进行遍历，添加元素
        list.stream().map((orders)->{
            //创建一个orderDto对象
            OrderDto dto = new OrderDto();
            //根据订单id调用订单详情表查询该订单一共多少件商品
            Integer count = orderDetailService.findCount(orders.getId());
            //将查询到的一共几件商品存入orderDto
            dto.setCount(count);
            //
            return dto;
        });
        //掉用dao进行分页查询，并返回查询的IPage对象
        return orderDao.selectPage(iPage,qw);
    }
}
